package com.qys.livesMall.app.user.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.app.user.model.dto.sys.*;
import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.app.user.model.entity.IpOperateStatistics;
import com.qys.livesMall.app.user.model.vo.WithdrawalsStatisticsVO;
import com.qys.livesMall.app.user.model.vo.sys.*;
import com.qys.livesMall.app.user.service.*;
import com.qys.livesMall.app.user.utils.IpLocation;
import com.qys.livesMall.app.user.utils.IpUtils;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.finance.model.entity.user.FinanceUserAccountDetail;
import com.qys.livesMall.finance.service.FinanceUserAccountDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@Tag(name = "sys-用户直播统计")
@RequestMapping("/sys/user/statistics")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysUserStatisticsController {

    private final UserStatisticsInfoService userStatisticsInfoService;

    private final IH2AppUserService ih2AppUserService;

     private final IChannelService iChannelService;

    private final IH2AppUserRechargeWithdrawalsService rechargeWithdrawalsService;

    private final IpOperateStatisticsService ipOperateStatisticsService;

    private final AppUserRelationService appUserRelationService;
    @PostMapping("userinfo/page")
    @Operation(summary = "分页查询用户统计信息")
    public R<Page<SysUserStatisticsVo>> userinfoPage(@RequestBody SysUserStatisticsPageDTO pageDTO) {
        Page<SysUserStatisticsVo> page=userStatisticsInfoService.pageSys(pageDTO);
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            List<String> registerIps = page.getRecords().stream().map(SysUserStatisticsVo::getRegisterIp).collect(Collectors.toList());
            Map<String,Integer> ipMap;
            if (CollectionUtils.isNotEmpty(registerIps)){
                List<IpOperateStatistics> ipOperateStatistics = ipOperateStatisticsService.getbyIps(registerIps,0);
                ipMap=ipOperateStatistics.stream().collect(Collectors.toMap(IpOperateStatistics::getIp,IpOperateStatistics::getNum));
            } else {
                ipMap = null;
            }
            page.getRecords().forEach(e -> {
                if (null != e.getSuperiorId()){
                    H2AppUser superiorAppUser = ih2AppUserService.getById(e.getSuperiorId());
                    if (null != superiorAppUser){
                        e.setSuperiorId(superiorAppUser.getUserRandomId());
                    }
                }
                if (StringUtils.isNotBlank(e.getRegisterIp()) && null != ipMap){
                    e.setIpRegisterNumber(ipMap.get(e.getRegisterIp()));
                }
                if (null != e.getRegisterIp() && null == e.getCountry()){
                    IpLocation ipLocation= IpUtils.getLocation(e.getRegisterIp());
                    e.setCountry(ipLocation.getCountry());
                }
                if (e.getFirstCharge() != null) {
                    Duration duration = Duration.between(e.getFirstCharge(), LocalDateTime.now());
                    //首充天数计算
                    int days = (int) duration.toDays();
                    e.setFirstChargeDay(days==0?1:days);
                }
                e.setNum(e.getDirectNumber()+e.getTwoNumber()+e.getThreeNumber());
            });
        }
        return R.ok(page);
    }

    @PostMapping("/updateNotes")
    @Operation(summary = "修改用户或者代理线备注")
    public R<?> addUserNotes(@RequestBody UserNotesAddDTO userNotesAddDTO){
        userStatisticsInfoService.addUserNotes(userNotesAddDTO);
        return R.ok();
    }


    @PostMapping("channel/list")
    @Operation(summary = "用户渠道集合")
    public R<List<UserChannelListVO>> channelList() {
        return R.ok(ih2AppUserService.channelList());
    }
    @PostMapping("channel/edit")
    @Operation(summary = "用户变更渠道")
    public R<Boolean> channelEdit(@RequestBody SysUserChannelEditDTO dto) {
        return R.ok(iChannelService.channelEdit(dto));
    }

    @PostMapping("recharge/page")
    @Operation(summary = "分页查询用户充值统计信息")
    public R<Page<RechargStatisticsVO>> rechargePage(@RequestBody RechargStatisticsPage pageDTO) {
        return R.ok(rechargeWithdrawalsService.rechargePage(pageDTO));
    }

    @PostMapping("recharge/state")
    @Operation(summary = "查询用户充值状态统计")
    public R<RechargStatisticsStateVO> rechargeState(@RequestBody RechargStatisticsPage pageDTO) {
        return R.ok(rechargeWithdrawalsService.rechargeState(pageDTO));
    }

    @PostMapping("withdrawals/page")
    @Operation(summary = "分页查询用户提现统计信息")
    public R<Page<WithdrawalsStatisticsVO>> withdrawalsPage(@RequestBody WithdrawalsStatisticsPage pageDTO) {
        return R.ok(rechargeWithdrawalsService.withdrawalsPage(pageDTO));
    }
}
